package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ModuleTableMapper;
import com.ruoyi.system.domain.ModuleTable;
import com.ruoyi.system.service.IModuleTableService;

/**
 * 模块Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-06
 */
@Service
public class ModuleTableServiceImpl implements IModuleTableService 
{
    @Autowired
    private ModuleTableMapper moduleTableMapper;

    /**
     * 查询模块
     * 
     * @param moduleId 模块主键
     * @return 模块
     */
    @Override
    public ModuleTable selectModuleTableByModuleId(Long moduleId)
    {
        return moduleTableMapper.selectModuleTableByModuleId(moduleId);
    }

    /**
     * 查询模块列表
     * 
     * @param moduleTable 模块
     * @return 模块
     */
    @Override
    public List<ModuleTable> selectModuleTableList(ModuleTable moduleTable)
    {
        return moduleTableMapper.selectModuleTableList(moduleTable);
    }

    /**
     * 新增模块
     * 
     * @param moduleTable 模块
     * @return 结果
     */
    @Override
    public int insertModuleTable(ModuleTable moduleTable)
    {
        if (moduleTable.getParentId() == null) {
            // 如果没有指定父级模块，默认为顶级模块
            moduleTable.setParentId(null);
        }
        moduleTable.setCreateTime(DateUtils.getNowDate());
        return moduleTableMapper.insertModuleTable(moduleTable);
    }

    /**
     * 修改模块
     * 
     * @param moduleTable 模块
     * @return 结果
     */
    @Override
    public int updateModuleTable(ModuleTable moduleTable)
    {
        moduleTable.setUpdateTime(DateUtils.getNowDate());
        return moduleTableMapper.updateModuleTable(moduleTable);
    }

    /**
     * 批量删除模块
     * 
     * @param moduleIds 需要删除的模块主键
     * @return 结果
     */
    @Override
    public int deleteModuleTableByModuleIds(Long[] moduleIds)
    {
        return moduleTableMapper.deleteModuleTableByModuleIds(moduleIds);
    }

    /**
     * 删除模块信息
     * 
     * @param moduleId 模块主键
     * @return 结果
     */
    @Override
    public int deleteModuleTableByModuleId(Long moduleId)
    {
        return moduleTableMapper.deleteModuleTableByModuleId(moduleId);
    }


    @Override
    public List<ModuleTable> selectModuleTreeList(ModuleTable moduleTable) {
        return moduleTableMapper.selectModuleTreeList(moduleTable);
    }


    @Override
    public int deleteModuleTableByModuleIdsRecursively(Long[] moduleIds) {
        // 获取所有子模块 ID
        List<Long> allSubModuleIds = getAllSubModuleIds(moduleIds);
        // 将数组转换为列表并合并
        List<Long> idsToDelete = List.of(moduleIds).stream()
                .collect(Collectors.toList());
        idsToDelete.addAll(allSubModuleIds);
        // 删除所有相关模块
        return moduleTableMapper.deleteModuleTableByModuleIds(idsToDelete.toArray(new Long[0]));
    }
    private List<Long> getAllSubModuleIds(Long[] moduleIds) {
        List<Long> subModuleIds = moduleTableMapper.selectModuleTableList(null)
                .stream()
                .filter(module -> module.getParentId() != null)
                .filter(module -> java.util.Arrays.stream(moduleIds).anyMatch(id -> id.equals(module.getParentId())))
                .map(ModuleTable::getModuleId)
                .collect(Collectors.toList());

        if (subModuleIds.isEmpty()) {
            return subModuleIds;
        } else {
            // 递归查找所有子模块
            subModuleIds.addAll(getAllSubModuleIds(subModuleIds.toArray(new Long[0])));
            return subModuleIds;
        }
    }

    @Override
    public boolean existsById(Long moduleId) {
        return moduleTableMapper.selectModuleTableByModuleId(moduleId) != null;
    }
}
